-- Copyright (c) 2015,2016 Lucky Byte, Inc.
--
-- Summary : 备份历史流水
--
-- Author  : 吴小虎 <xiaohu@lucky-byte.com>
-- Since   : 2016.9.14
--
-- History
--  * 2016.9.14 : 创建此文件
--  * 2016.9.28 : 转移今天之前的所有流水记录
--  * 2016.9.28 : 转移助农取款订单记录
--
-- today = core.date()
-- date_point = today:adddays(-10)

-- 转移流水记录
jdbc.update([[
    with result as (
        delete from pay_trlist where sys_time::date < CURRENT_DATE returning *
    ) insert into pay_trlist_hist (
        serial, uuid, uuid_no, sys_time,
        incoming, chnl, indirect, trinfo, merch, status, netinfo,
        i_pkt, o_pkt, n_pkt, flags, fee, dcflag, notes
    ) (
        select serial, uuid, uuid_no, sys_time,
        	incoming, chnl, indirect, trinfo, merch, status, netinfo,
        	i_pkt, o_pkt, n_pkt, flags, fee, dcflag, notes
        from result
    )
]])

-- 转移助农取款订单记录
jdbc.update([[
    with result as (
        delete from pay_p2_order where sys_time::date < CURRENT_DATE returning *
    ) insert into pay_p2_order_hist (
        serial, uuid, sys_time, p2_uuid, t60_uuid, t40_uuid,
        p2_fee, fee_take_from, p2_fund_uuid,
        recover, recvr_uuid, t40_uuid_old,
        status, received, notes
    ) (
        select serial, uuid, sys_time, p2_uuid, t60_uuid, t40_uuid,
            p2_fee, fee_take_from, p2_fund_uuid,
            recover, recvr_uuid, t40_uuid_old,
            status, received, notes
        from result
    )
]])

-- 转移助农取款资金池变动历史
jdbc.update([[
    with result as (
        delete from pay_p2_fund_alter where ctime::date < CURRENT_DATE returning *
    ) insert into pay_p2_fund_alter_hist (
        serial, ctime, p2_fund, type, remains, amt, merno, termid, notes
    ) (
        select serial, ctime, p2_fund, type, remains, amt, merno, termid, notes
        from result
    )
]])
